<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
        <el-form-item label="考生号" prop="nickname">
          <el-input v-model="dataForm.number" placeholder="考生号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
        </el-form-item>
        <el-form-item label="中学名称" prop="cultureGrade">
          <el-input v-model="dataForm.middleSchool" placeholder="中学名称"></el-input>
        </el-form-item>
        <el-form-item label="考试分类" prop="skillGrade">
          <el-input v-model="dataForm.category" placeholder="考试分类"></el-input>
        </el-form-item>
        <el-form-item label="考试科目" prop="totalPoints">
          <el-input v-model="dataForm.subject" placeholder="考试科目"></el-input>
        </el-form-item>
        <el-form-item label="考试时间" prop="totalPoints">
          <el-input v-model="dataForm.time" placeholder="考试时间"></el-input>
        </el-form-item>
        <el-form-item label="考试日期" prop="totalPoints">
          <el-input v-model="dataForm.date" placeholder="考试日期"></el-input>
        </el-form-item>
        <el-form-item label="考试地点" prop="totalPoints">
          <el-input v-model="dataForm.address" placeholder="考试地点"></el-input>
        </el-form-item>
        <el-form-item label="考场号" prop="totalPoints">
          <el-input v-model="dataForm.addressId" placeholder="考场号"></el-input>
        </el-form-item>
        <el-form-item label="考场名称" prop="totalPoints">
          <el-input v-model="dataForm.addressName" placeholder="考场名称"></el-input>
        </el-form-item>
        <el-form-item label="座位号" prop="totalPoints">
          <el-input v-model="dataForm.seat" placeholder="座位号"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="totalPoints">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
  </el-dialog>
</template>

<script>
import {admissionGetById, admissionSave, admissionUpdate} from "@/request/utils/admission";

  export default {
    data () {
      return {
        visible: false,
        newUserId: 0,
        dataForm: {
          number: '',
          nickname: '',
          idCard: '',
          sex: '',
          middleSchool: '',
          category: '',
          subject: '',
          time: '',
          date: '',
          address: '',
          addressId: '',
          addressName: '',
          seat: '',
          remark: '',
        },
        dataRule: {
          number: [
            { required: true, message: '考生号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          middleSchool: [
            { required: true, message: '中学名称不能为空', trigger: 'blur' }
          ],
          category: [
            { required: true, message: '考试分类不能为空', trigger: 'blur' }
          ],
          subject: [
            { required: true, message: '考试科目不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '考试时间不能为空', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '考试日期不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '考试地点不能为空', trigger: 'blur' }
          ],
          addressId: [
            { required: true, message: '考场号不能为空', trigger: 'blur' }
          ],
          addressName: [
            { required: true, message: '考场名称不能为空', trigger: 'blur' }
          ],
          seat: [
            { required: true, message: '座位号不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
        }
      }
    },

    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            admissionGetById(this.dataForm.id).then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.dataForm.id===undefined){
              admissionSave(this.dataForm).then(data => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                  this.dataForm = {}
                } else {
                  this.$message.error(data.msg)
                }
              })
            }else {
              admissionUpdate(this.dataForm).then(data => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                  this.dataForm = {}
                } else {
                  this.$message.error(data.msg)
                }
              })
            }

          }
        })
      }
    }
  }
</script>
